#!/usr/bin/env bash

# HI3 patch by mkrsym1
# https://notabug.org/mkrsym1/dusk

script_dir=`dirname $0`
source "${script_dir}"/common

if [ "$(realpath "${script_dir}")" == "$(realpath .)" ]; then
    error "Do not put the patch installer into the game directory!"
fi

root_cmd="pkexec"
yes_to_all=""
# Parse commandline arguments
while [ "x$1" != "x" ];
do
    case $1 in
        --root-cmd)
            shift
            root_cmd="$1"
            ;;
        --yes-to-all)
            yes_to_all="1"
            ;;
        *)
            error "Invalid commandline argmument: $1"
            ;;
    esac
    shift
done


# File checksums
bh3base_checksum=($(md5sum BH3Base.dll))
if [ "${bh3base_checksum}" != "a8a9faa460be9c6fd1d0a0f7fe62a678" ]; then
    error "BH3Base.dll: wrong file version or file is already patched. Run uninstall.sh to remove the patch."
fi

unityplayer_checksum=($(md5sum UnityPlayer.dll))
if [ "${unityplayer_checksum}" != "27fbee826303858eb28ccd5dc4a67b43" ]; then
    error "UnityPlayer.dll: wrong file version or file is already patched. Run uninstall.sh to remove the patch."
fi

if [ "${yes_to_all}" != "1" ]; then
    echo
    echo "Modifying the game is a violation of it's Terms of Service! Make sure that you understand all of the risks and possible consequences before proceeding!"
    read -p "Do you wish to proceed? [y/N] " response
    if [[ ! "$response" == [Yy]* ]]; then
        exit 1
    fi
fi

# Remove after testing
if [ "${yes_to_all}" != "1" ]; then
    echo
    echo "This patch is very new. There is a serious risk of your account getting banned!"
    read -p "Do you still wish to proceed? [y/N] " response
    if [[ ! "$response" == [Yy]* ]]; then
        exit 1
    fi
fi

echo
info "Installing the patch"

logging_servers=$(cat <<EOF
# Honkai Impact 3rd logging servers
0.0.0.0 log-upload-os.hoyoverse.com
0.0.0.0 sg-public-data-api.hoyoverse.com
0.0.0.0 dump.gamesafe.qq.com
EOF
)

echo
if [[ ! `cat /etc/hosts` == *"$logging_servers"* ]]; then
    info "Blocking logging servers. This will require superuser privileges (canceling will skip this step)"
    echo "$logging_servers" | $root_cmd tee -a /etc/hosts 2>&1 >> /dev/null
    if test $? -ne 0; then
        warn "Could not block logging servers. Please add the following lines to your /etc/hosts manually:"
        echo "$logging_servers"
    fi
else
    info "Logging servers are already blocked"
fi

echo
info "Copying additional files"
for additional_file in ${additional_files[@]}
do
    info "Copying ${additional_file}"
    cp -r "${script_dir}/files/${additional_file}" .
done

echo
info "Pathing game files"
for game_file in ${game_files[@]}
do
    if test -f "${game_file}"; then
        info "Patching ${game_file}"
        
        xdelta3 -d -s "${game_file}" "${script_dir}/diffs/${game_file}.vcdiff" "${game_file}.p"
        mv "${game_file}" "${game_file}.bak"
        mv "${game_file}.p" "${game_file}"
    else
        error "${game_file} not found!"
    fi
done

echo
info "Done"
echo
echo "!!! PLEASE DON'T SHARE THIS PROJECT IN PUBLIC !!!"

echo
